package cn.com.swan.framework.module.userrole.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import cn.com.swan.framework.dao.BaseDao;
import cn.com.swan.framework.exception.SwanException;
import cn.com.swan.framework.global.PageBean;
import cn.com.swan.framework.module.userrole.model.Userrole;
import cn.com.swan.framework.module.userrole.service.UserroleManager;

public class UserroleManagerImpl implements UserroleManager {
	
	BaseDao baseDao = null;

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}


	public void createUserrole(Userrole Userrole) throws SwanException {
		getBaseDao().create(Userrole);
		
	}

	public void deleteUserrole(Userrole Userrole) throws SwanException {
		getBaseDao().delete(Userrole);		
	}

	public void updateUserrole(Userrole Userrole) throws SwanException {
		getBaseDao().update(Userrole);
		
	}

	public List<Userrole> queryUserrole(Userrole Userrole,
			PageBean pageBean, String orderBy, Boolean isAsc)
			throws SwanException {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Userrole.class);
		if(Userrole.getUserId()!=null && !"".equals(Userrole.getUserId())){
//			detachedCriteria.add(Restrictions.like("userId", Userrole.getUserId(),MatchMode.ANYWHERE));
			detachedCriteria.add(Restrictions.eq("userId", Userrole.getUserId()));
		}
		if(Userrole.getRoleId()!=null && !"".equals(Userrole.getRoleId())){
//			detachedCriteria.add(Restrictions.like("userId", Userrole.getUserId(),MatchMode.ANYWHERE));
			detachedCriteria.add(Restrictions.eq("roleId", Userrole.getRoleId()));
		}
		
		return getBaseDao().queryByCriteria(detachedCriteria, pageBean, orderBy, isAsc);
	}

	public Userrole queryUserrole(String entityId) throws SwanException {
		return (Userrole) getBaseDao().load(Userrole.class, entityId);
	}

}
